home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / textbox.c < prev    next >
C/C++ Source or Header  |  1996-07-31  |  5KB  |  260 lines

  1. #include "diadef.h"
  2. #include "dialog.h"
  3. #include "internal.h"
  4.  
  5.  
  6. class FIELD_TEXTBOX: public FIELD_STRING{
  7.     /*~PROTOBEG~ FIELD_TEXTBOX */
  8. public:
  9.     FIELD_TEXTBOX (char *_buf);
  10.     void drawtxt (WINDOW *dialog);
  11.     void html_draw (int);
  12.     int html_validate (int);
  13.     /*~PROTOEND~ FIELD_TEXTBOX */
  14. };
  15.  
  16. PUBLIC FIELD_TEXTBOX::FIELD_TEXTBOX(
  17.     char *_buf)
  18.     : FIELD_STRING ("",_buf,strlen(_buf))
  19. {
  20.     readonly = 1;
  21.     box.width = strlen(buf);
  22.     if (box.width > 75) box.width = 75;
  23. }
  24.  
  25. /*
  26.     Draw one text string if its len > 0
  27. */
  28. static void textbox_drawif(
  29.     char *start,
  30.     char *instr,
  31.     WINDOW *dialog)
  32. {
  33.     if (start != instr){
  34.         char tmp = *instr;
  35.         *instr = '\0';
  36.         wattrset(dialog, inputbox_attr);
  37.         waddstr (dialog,start);
  38.         *instr = tmp;
  39.     }
  40. }
  41.  
  42.  
  43. static int hoffset = 0;
  44. /*
  45.     Draw only the input part of a field
  46. */
  47. PUBLIC void FIELD_TEXTBOX::drawtxt (WINDOW *dialog)
  48. {
  49.     int blank_start = 0;
  50.     wmove(dialog, box.y,box.x);
  51.     const int box_width = box.width;
  52.     if (hoffset < size){
  53.         /* #Specification: textbox / underlining and highlit
  54.             Text may contain underlining commands
  55.             and highliting commands built using the
  56.             backspace character.
  57.  
  58.             #
  59.             Highlit  : X^HX
  60.             Underline: X^H_
  61.             #
  62.         */
  63.         char *instr = buf;
  64.         int offset = 0;
  65.         while (*instr != '\0' && offset < hoffset){
  66.             if (*instr == 8){
  67.                 offset -= 2;
  68.                 instr++;
  69.             }
  70.             offset++;
  71.             instr++;
  72.         }
  73.         char *start = instr;
  74.         while (*instr != '\0' && blank_start < box_width){
  75.             if (instr[1] == 8){
  76.                 textbox_drawif (start,instr,dialog);
  77.                 if (instr[0] == '_'){
  78.                     // Underline ... does not work
  79.                     wattrset(dialog, A_UNDERLINE);
  80.                     waddch (dialog,instr[2]);
  81.                 }else{
  82.                     // Highlit
  83.                     wattrset(dialog, button_active_attr);
  84.                     waddch (dialog,instr[0]);
  85.                 }
  86.                 instr += 2;
  87.                 start = instr+1;
  88.             }
  89.             blank_start++;
  90.             instr++;
  91.         }
  92.         textbox_drawif (start,instr,dialog);
  93.     }
  94.     wattrset(dialog, inputbox_attr);
  95.     for (int i=blank_start; i<box_width; i++) waddch (dialog,' ');
  96. }
  97. PUBLIC void FIELD_TEXTBOX::html_draw (int )
  98. {
  99.     html_printf ("<tr><td>%s\n",buf);
  100. }
  101. PUBLIC int FIELD_TEXTBOX::html_validate (int )
  102. {
  103.     return 0;
  104. }
  105.  
  106. /*
  107.     Expand tab character (8) and translate some others
  108. */
  109. void textbox_expandtab(
  110.     const char *src,
  111.     char *dst,
  112.     int maxsiz)
  113. {
  114.     int pos = 0;
  115.     while (*src != '\0' && pos < maxsiz){
  116.         char carac = *src++;
  117.         if (carac == '\t'){
  118.             if (pos % 8 == 0){
  119.                 pos++;
  120.                 *dst++ = ' ';
  121.             }
  122.             while (pos % 8){
  123.                 pos++;
  124.                 *dst++ = ' ';
  125.             }
  126.         }else if (carac == 7){
  127.             // Some bullets placed by linuxdoc-sgml
  128.             *dst++ = '-';
  129.         }else{
  130.             *dst++ = carac;
  131.             pos++;
  132.         }
  133.     }
  134.     *dst = '\0';
  135. }
  136.  
  137. class  DIALOG_TEXTBOX: public DIALOG{
  138.     /*~PROTOBEG~ DIALOG_TEXTBOX */
  139. public:
  140.     DIALOG_TEXTBOX (void);
  141. protected:
  142.     int keymove (WINDOW *dialog,
  143.          int key,
  144.          int &nof);
  145. public:
  146.     /*~PROTOEND~ DIALOG_TEXTBOX */
  147. };
  148.  
  149. PUBLIC DIALOG_TEXTBOX::DIALOG_TEXTBOX()
  150. {
  151.     hoffset = 0;
  152. }
  153.  
  154.  
  155.  
  156. PROTECTED int DIALOG_TEXTBOX::keymove (WINDOW *dialog, int key, int &nof)
  157. {
  158.     int ret = 0;
  159.     switch (key){
  160.     case KEY_HOME:
  161.         if (hoffset > 0){
  162.             hoffset = 0;
  163.             drawf (dialog);
  164.         }
  165.         break;
  166.     case KEY_LEFT:
  167.         if (hoffset > 0){
  168.             hoffset--;
  169.             drawf (dialog);
  170.         }
  171.         break;
  172.     case KEY_RIGHT:
  173.         hoffset++;
  174.         drawf (dialog);
  175.         break;
  176.     case KEY_DOWN:
  177.         // Force a scroll
  178.         nof = offset + nbvisible-1;
  179.         ret = DIALOG::keymove (dialog,key,nof);    
  180.         nof = offset;
  181.         break;
  182.     case KEY_UP:
  183.         // Force a scroll
  184.         nof = offset;
  185.     default:
  186.         ret = DIALOG::keymove (dialog,key,nof);    
  187.     }
  188.     return ret;
  189. }
  190. /*
  191.     Display text from a file in a dialog box.
  192. */
  193. MENU_STATUS dialog_textbox(
  194.     const char *title,
  195.     const char *file)
  196. {
  197.     FILE *fin = xconf_fopen (file,"r");
  198.     MENU_STATUS ret = MENU_ESCAPE;
  199.     if (fin != NULL){
  200.         DIALOG_TEXTBOX dia;
  201.         char buf[300];
  202.         while (fgets(buf,sizeof(buf)-1,fin)!=NULL){
  203.             str_strip (buf,buf);
  204.             char buftab[600];
  205.             textbox_expandtab (buf,buftab,sizeof(buftab)-1);
  206.             dia.add (new FIELD_TEXTBOX(buftab));
  207.         }
  208.         fclose (fin);
  209.         int nof = 0;
  210.         ret = dia.edit (title,NULL,NULL,nof,MENUBUT_QUIT);
  211.     }
  212.     return ret;
  213. }
  214.  
  215. /*
  216.     Display text from a table of strings.
  217. */
  218. MENU_STATUS dialog_textbox(
  219.     const char *title,
  220.     const SSTRINGS &strs)
  221. {
  222.     DIALOG_TEXTBOX dia;
  223.     for (int i=0; i<strs.getnb(); i++){
  224.         SSTRING *str = strs.getitem(i);
  225.         char buf[600];
  226.         str->copy (buf);
  227.         str_strip (buf,buf);
  228.         char buftab[600];
  229.         textbox_expandtab (buf,buftab,sizeof(buftab)-1);
  230.         dia.add (new FIELD_TEXTBOX(buftab));
  231.     }
  232.     int nof = 0;
  233.     return dia.edit (title,NULL,NULL,nof,MENUBUT_QUIT);
  234. }
  235.  
  236.  
  237. #ifdef TEST
  238.  
  239. int main (int argc, char *argv[])
  240. {
  241.     int ret;
  242.     init_dialog();
  243.     ret  = dialog_textbox("file /tmp/toto.c"
  244.         ,"/tmp/toto.c"
  245.         ,15,50);
  246.     endwin();
  247.     printf ("ret = %d\n",ret);
  248.     return 0;
  249. }
  250.  
  251. #endif
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.